const enterpriseVue = new Vue({
    el: '#app',
    data() {
      return {
        pickerOptions: pickerOptions,
        rules: {
          Start_Date: [{ required: true, message: '请选择开始时间', trigger: 'blur' }],
        },
        dealer: [],
        shop: [],
        dialogVisible: false,
        form: {
          Dealer_Unit_Price_SerialNumber: "",
          Start_Date: '',
          End_Date: '',
          Enterprise_Info_SerialNumber: $.cookie("enterprise"),
          Children: {
            dealer_unitprice_detail: [{
              Dealer_Unit_Price_Detail_SerialNumber: "",
              Dealer_Unit_Price_SerialNumber:'',
              Article_SerialNumber:'',
              Unit_Price:'',
              Balance_Unit_Price:'',
              Daily_Supply:'',
              Enterprise_Info_SerialNumber:$.cookie("enterprise"),
            }],
            dealer_unitprice_storescope: [{
              Dealer_Unit_Price_StoreScope_SerialNumber: "",
              Dealer_Unit_Price_SerialNumber: '',
              Dealer_SerialNumber: '',
              Enterprise_Info_SerialNumber:$.cookie("enterprise"),
            }],
          }
        },
        loading: false,
        tableData: [],
      }
    },
    mounted() {
      this.getData()
    },
    methods: {
      getData() {
        const that = this;
        $.ajax({
          type: 'POST',
          url: DomainName + "/getInquiryAction?token="+$.cookie('token'),
          data: {
            Data: "dealer_info",
          },
          success: function (res) {
            that.dealer = res;
          },
          complete: function (XMLHttpRequest) {
            that.loading = false
            if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
              window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
            }
          },
          dataType: "json"
        });
        $.ajax({
          url: DomainName + '/getInquiryAction?token=' +  $.cookie("token"),
          type: 'POST',
          data: { 
            Data:"article_info",
            Row_Status:"<>'2'"
          },
          success: function (res) {
            that.shop = res
          },
          complete: function (XMLHttpRequest) {
            that.loading = false
            if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
              window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
            }
          }
        })
        
      },
      validateEnd_Date(rule, value, callback) {
        if (+new Date(value) < +new Date(this.form.Start_Date)) {
          callback(new Error('结算时间不能小于开始时间'));
        } else {
          callback();
        }
      },
      Change_Unit_Price(e,i) {
        let str = this.shop.filter(i=>i.Article_SerialNumber == e)[0].Unit_Price
        this.form.Children.dealer_unitprice_detail[i].Unit_Price = str
      },
      //获取外企也商品主键
      submitForm(formName) {
        const that = this
        this.$refs[formName].validate((valid) => {
          if (valid) {
            that.loading = true
            $.ajax({
              url: DomainName + '/getAddAction?token='+ $.cookie("token"),
              type: 'POST',
              data: {
                Data: JSON.stringify({
                  dealer_unitprice: [that.form]
                }),
              },
              success: function (res) {
                if (res.Status == 'success') {
                  that.$message.success("操作成功")
                  window.history.go(-1);
                } else {
                  that.$message.error("操作失败，请稍后再试")
                }
              },
              error: function () {
                that.$message.error("操作失败，请稍后再试")
              },
              complete: function (XMLHttpRequest) {
                that.loading = false
                if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                  window.parent.document.location.href = absolutepath() + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                }
              }
            })
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      onExcel() {
        this.dialogVisible = true
      },
      add_dealer_unitprice_storescope() {
        this.form.Children.dealer_unitprice_storescope.push({
          Dealer_Unit_Price_StoreScope_SerialNumber: "",
          Dealer_Unit_Price_SerialNumber: '',
          Dealer_SerialNumber: '',
          Enterprise_Info_SerialNumber:$.cookie("enterprise"),
        })
      },
      remove_dealer_unitprice_storescope() {
        if (this.form.Children.dealer_unitprice_storescope.length > 1) {
          this.form.Children.dealer_unitprice_storescope.pop()
        } else {
          this.$message({
            message: '不能在删除了',
            type: 'warning'
          })
        }
      },
      add_dealer_unitprice_detail() {
        this.form.Children.dealer_unitprice_detail.push({
          Dealer_Unit_Price_Detail_SerialNumber: "",
          Dealer_Unit_Price_SerialNumber:'',
          Article_SerialNumber:'',
          Unit_Price:'',
          Balance_Unit_Price:'',
          Daily_Supply:'',
          Enterprise_Info_SerialNumber:$.cookie("enterprise"),
        })
      },
      remove_dealer_unitprice_detail() {
        if (this.form.Children.dealer_unitprice_detail.length > 1) {
          this.form.Children.dealer_unitprice_detail.pop()
        } else {
          this.$message({
            message: '不能在删除了',
            type: 'warning'
          })
        }
      },
    }
  })